$(document).ready(function() {
    var flag = parseInt($("#flagaction").val());
    var url = $("#url").val();
    if (flag == 1) {
        scannerid(flag + 1, url);
    }
    flag = parseInt($("#flagaction").val());
    if (flag == 2) {
        loadpagevideos(flag + 1, url);

    }
    flag = parseInt($("#flagaction").val());
    if (flag == 3) {
        loadvideos(url, 1);
    }
    // alert(flag);
});
function scannerid(flag, url) {
    var urlfull = window.location.href;
    var data = urlfull.split("/").slice(-1).pop().split(".");
    var linkcatalog = data[0];
    $.ajax({
        url: url + "/scannercatalogid/scannercatalog",
        type: "POST",
        cache: true,
        async: false,
        data: "linkcatalog=" + linkcatalog,
        success: function(dataserver) {
            var data = dataserver.split("*duypc*");
            if (data[0] != "") {
                $("#flagaction").val(flag);
                $("#listcatalogid").val(dataserver);
                $("#titlecatalog").html('<h2><a href="javascript:void(0);"><i class="fa fa-bars"></i> ' + data[1] + '.</a></h2>');
            } else {
                $("#titlecatalog").html('<h2><a href="javascript:void(0);"><i class="fa fa-bars"></i> Chuyên mục này không tồn tại.</a></h2>');
            }
        }
    });
}
function loadpagevideos(flag, url) {
    var pageget = $.urlParam("page");
    var page = "1";
    if (pageget != null) {
        page = pageget;
    }
    var listcatalog = $("#listcatalogid").val();
    $.ajax({
        url: url + "/scannercatalogid/loadpage",
        type: "POST",
        cache: true,
        async: false,
        data: "listcatalog=" + listcatalog + "&page=" + page,
        success: function(dataserver) {
            $("#flagaction").val(flag);
            $("#pagevideos").html(dataserver);

        }
    });
}
function loadvideos(url, page) {
    var pageget = $.urlParam("page");
    if (pageget != null) {
        page = pageget;
    }
    var listcatalog = $("#listcatalogid").val();
    $.ajax({
        url: url + "/scannercatalogid/loadvideo",
        type: "POST",
        cache: true,
        async: false,
        data: "listcatalog=" + listcatalog + "&page=" + page,
        success: function(dataserver) {
            $("#listvideosview").html(dataserver);
        }
    });
}
function pagevideos(page) {
    var urlfull = window.location.href;
    var dataurl = urlfull.split("?");
    window.history.replaceState({}, 'page', dataurl[0] + '?page=' + page);
    $("#pagevideos").find("a").removeClass("activepage");
    $("#page" + page).addClass("activepage");
    var url = $("#url").val();
    loadvideos(url, page);

}
$.urlParam = function(name) {
    var results = new RegExp('[\?&amp;]' + name + '=([^&amp;#]*)').exec(window.location.href);
    if (results == null) {
        return null;
    } else {
        return results[1] || 0;
    }
}